{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEBUG:matplotlib.pyplot:Loaded backend module://matplotlib_inline.backend_inline version unknown.\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import IPython.display as ipd\n",
    "\n",
    "import os\n",
    "import json\n",
    "import math\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "from torch.utils.data import DataLoader\n",
    "\n",
    "import commons\n",
    "import utils\n",
    "from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate\n",
    "from models import SynthesizerTrn\n",
    "from text.symbols import symbols\n",
    "from text import text_to_sequence\n",
    "\n",
    "from scipy.io.wavfile import write\n",
    "\n",
    "\n",
    "def get_text(text, hps):\n",
    "    text_norm = text_to_sequence(text, hps.data.text_cleaners)\n",
    "    if hps.data.add_blank:\n",
    "        text_norm = commons.intersperse(text_norm, 0)\n",
    "    text_norm = torch.LongTensor(text_norm)\n",
    "    return text_norm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Speaker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "hps = utils.get_hparams_from_file(\"configs/NickYuan2.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[10], line 8\u001b[0m\n\u001b[0;32m      1\u001b[0m net_g \u001b[39m=\u001b[39m SynthesizerTrn(\n\u001b[0;32m      2\u001b[0m     \u001b[39mlen\u001b[39m(symbols),\n\u001b[0;32m      3\u001b[0m     hps\u001b[39m.\u001b[39mdata\u001b[39m.\u001b[39mfilter_length \u001b[39m/\u001b[39m\u001b[39m/\u001b[39m \u001b[39m2\u001b[39m \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m,\n\u001b[0;32m      4\u001b[0m     hps\u001b[39m.\u001b[39mtrain\u001b[39m.\u001b[39msegment_size \u001b[39m/\u001b[39m\u001b[39m/\u001b[39m hps\u001b[39m.\u001b[39mdata\u001b[39m.\u001b[39mhop_length,\n\u001b[0;32m      5\u001b[0m     \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mhps\u001b[39m.\u001b[39mmodel)\u001b[39m.\u001b[39mcuda()\n\u001b[0;32m      6\u001b[0m _ \u001b[39m=\u001b[39m net_g\u001b[39m.\u001b[39meval()\n\u001b[1;32m----> 8\u001b[0m _ \u001b[39m=\u001b[39m utils\u001b[39m.\u001b[39;49mload_checkpoint(\u001b[39m\"\u001b[39;49m\u001b[39mlogs/NickYuan2/G_13000.pth.pth\u001b[39;49m\u001b[39m\"\u001b[39;49m, net_g, \u001b[39mNone\u001b[39;49;00m)\n",
      "File \u001b[1;32me:\\AIGC\\apps\\vits2\\utils.py:19\u001b[0m, in \u001b[0;36mload_checkpoint\u001b[1;34m(checkpoint_path, model, optimizer)\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mload_checkpoint\u001b[39m(checkpoint_path, model, optimizer\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m):\n\u001b[1;32m---> 19\u001b[0m   \u001b[39massert\u001b[39;00m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39misfile(checkpoint_path)\n\u001b[0;32m     20\u001b[0m   checkpoint_dict \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mload(checkpoint_path, map_location\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mcpu\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[0;32m     21\u001b[0m   iteration \u001b[39m=\u001b[39m checkpoint_dict[\u001b[39m'\u001b[39m\u001b[39miteration\u001b[39m\u001b[39m'\u001b[39m]\n",
      "\u001b[1;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "net_g = SynthesizerTrn(\n",
    "    len(symbols),\n",
    "    hps.data.filter_length // 2 + 1,\n",
    "    hps.train.segment_size // hps.data.hop_length,\n",
    "    **hps.model).cuda()\n",
    "_ = net_g.eval()\n",
    "\n",
    "_ = utils.load_checkpoint(\"logs/NickYuan2/G_13000.pth\", net_g, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'net_g' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 5\u001b[0m\n\u001b[0;32m      3\u001b[0m     x_tst \u001b[39m=\u001b[39m stn_tst\u001b[39m.\u001b[39mcuda()\u001b[39m.\u001b[39munsqueeze(\u001b[39m0\u001b[39m)\n\u001b[0;32m      4\u001b[0m     x_tst_lengths \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mLongTensor([stn_tst\u001b[39m.\u001b[39msize(\u001b[39m0\u001b[39m)])\u001b[39m.\u001b[39mcuda()\n\u001b[1;32m----> 5\u001b[0m     audio \u001b[39m=\u001b[39m net_g\u001b[39m.\u001b[39minfer(x_tst, x_tst_lengths, noise_scale\u001b[39m=\u001b[39m\u001b[39m.667\u001b[39m, noise_scale_w\u001b[39m=\u001b[39m\u001b[39m0.8\u001b[39m, length_scale\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)[\u001b[39m0\u001b[39m][\u001b[39m0\u001b[39m,\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39mdata\u001b[39m.\u001b[39mcpu()\u001b[39m.\u001b[39mfloat()\u001b[39m.\u001b[39mnumpy()\n\u001b[0;32m      6\u001b[0m ipd\u001b[39m.\u001b[39mdisplay(ipd\u001b[39m.\u001b[39mAudio(audio, rate\u001b[39m=\u001b[39mhps\u001b[39m.\u001b[39mdata\u001b[39m.\u001b[39msampling_rate, normalize\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m))\n",
      "\u001b[1;31mNameError\u001b[0m: name 'net_g' is not defined"
     ]
    }
   ],
   "source": [
    "stn_tst = get_text(\"在声音设计方面，剧集在部分安静场景中放大了环境音，凸显僵持、严峻的形势；在拷问、对峙等场景中，节奏急促的背景音效则持续铺垫其中，使得紧张的气息扑面而来。\", hps)\n",
    "with torch.no_grad():\n",
    "    x_tst = stn_tst.cuda().unsqueeze(0)\n",
    "    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
    "    audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy()\n",
    "ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate, normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple Speakers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hps = utils.get_hparams_from_file(\"./configs/XXX.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net_g = SynthesizerTrn(\n",
    "    len(symbols),\n",
    "    hps.data.filter_length // 2 + 1,\n",
    "    hps.train.segment_size // hps.data.hop_length,\n",
    "    n_speakers=hps.data.n_speakers,\n",
    "    **hps.model).cuda()\n",
    "_ = net_g.eval()\n",
    "\n",
    "_ = utils.load_checkpoint(\"/path/to/model.pth\", net_g, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stn_tst = get_text(\"こんにちは\", hps)\n",
    "with torch.no_grad():\n",
    "    x_tst = stn_tst.cuda().unsqueeze(0)\n",
    "    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
    "    sid = torch.LongTensor([4]).cuda()\n",
    "    audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy()\n",
    "ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate, normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Voice Conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = TextAudioSpeakerLoader(hps.data.validation_files, hps.data)\n",
    "collate_fn = TextAudioSpeakerCollate()\n",
    "loader = DataLoader(dataset, num_workers=8, shuffle=False,\n",
    "    batch_size=1, pin_memory=True,\n",
    "    drop_last=True, collate_fn=collate_fn)\n",
    "data_list = list(loader)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with torch.no_grad():\n",
    "    x, x_lengths, spec, spec_lengths, y, y_lengths, sid_src = [x.cuda() for x in data_list[0]]\n",
    "    sid_tgt1 = torch.LongTensor([1]).cuda()\n",
    "    sid_tgt2 = torch.LongTensor([2]).cuda()\n",
    "    sid_tgt3 = torch.LongTensor([4]).cuda()\n",
    "    audio1 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt1)[0][0,0].data.cpu().float().numpy()\n",
    "    audio2 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt2)[0][0,0].data.cpu().float().numpy()\n",
    "    audio3 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt3)[0][0,0].data.cpu().float().numpy()\n",
    "print(\"Original SID: %d\" % sid_src.item())\n",
    "ipd.display(ipd.Audio(y[0].cpu().numpy(), rate=hps.data.sampling_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt1.item())\n",
    "ipd.display(ipd.Audio(audio1, rate=hps.data.sampling_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt2.item())\n",
    "ipd.display(ipd.Audio(audio2, rate=hps.data.sampling_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt3.item())\n",
    "ipd.display(ipd.Audio(audio3, rate=hps.data.sampling_rate, normalize=False))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "c15292341d300295ca9f634d04c483f667a0c1d5ee0c309c2ac4e312cce8b8df"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
